#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:申请企业未结清对外担保五级分类校验
code:Ent_UnpaidGuaranClass5State_Check
tableName:
columnName:
groups:准入>企业征信校验
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
ECRSummaryGuarantee:对外担保信息概要
GuaranteedBalance_Bad:被担保业务余额(不良)
GuaranteedBalance_Concerned:被担保业务余额(关注)
'''

import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')

def Ent_UnpaidGuaranClass5State_Check():
    try:
        null_type_list = ['', None, 'null', 'Null', 'NULL', '/','--','-']
        balance = []
        if EC in null_type_list:
            return u'缺失值'
        if len(EC) == 0:
            return u'缺失值'
        ecr = json.loads(EC[0]["ecr"])
        if ecr in null_type_list:
            return u'缺失值'
        if len(ecr) == 0:
            return u'缺失值'
        if ecr.has_key('ECRSummaryGuarantee') and ecr['ECRSummaryGuarantee'] not in null_type_list:
            try:
                if ecr['ECRSummaryGuarantee'][0].has_key('GuaranteedBalance_Bad') \
                    and float(ecr['ECRSummaryGuarantee'][0]['GuaranteedBalance_Bad'])>0:
                        balance.append(float(ecr['ECRSummaryGuarantee'][0]['GuaranteedBalance_Bad']))
                else:
                    balance.append(None)
            except:
                    balance.append(None)

            try:
                if ecr['ECRSummaryGuarantee'][0].has_key('GuaranteedBalance_Concerned') \
                    and float(ecr['ECRSummaryGuarantee'][0]['GuaranteedBalance_Concerned'])>0:
                        balance.append(float(ecr['ECRSummaryGuarantee'][0]['GuaranteedBalance_Concerned']))
                else:
                    balance.append(None)
            except:
                balance.append(None)
        #若count均为None,则返回缺失值,否则将None置为0
        balance_num = filter(lambda x: x is not None,balance)
        return round(sum(balance_num),4) if len(balance_num) != 0 else u'缺失值'
    except:
        return u'缺失值'

result = Ent_UnpaidGuaranClass5State_Check()